package org.sbbs.demo.webapp.action;

import javax.servlet.http.HttpServletRequest;

import org.sbbs.base.webapp.action.BaseMaintainAction;
import org.sbbs.demo.model.DemoEntity;
import org.sbbs.demo.service.DemoEntityManager;

public class DemoEntityMaintainAction
    extends BaseMaintainAction<DemoEntity, Long> {

    public String edit() {
        this.setEditType( EDITTYPE_EDIT );
        this.setModel( this.demoEntityManager.find( this.getId() ) );
        return this.SUCCESS;
    }

    public String add() {
        this.setEditType( EDITTYPE_EDIT );
        this.setModel( new DemoEntity() );
        return this.SUCCESS;
    }

    public String save() {
        try {
            HttpServletRequest req = this.getRequest();
            boolean isNew = ( this.getEditType() == 1 );
            this.getDemoEntityManager().save( this.getModel() );
            this.setAjaxMessage( getText( ( isNew ) ? "demoEntity.added" : "demoEntity.updated", "no msg key found,save successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.saved", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }
        return this.SUCCESS;
    }

    public String delete() {
        try {
            String[] sIds = this.getIds().split( "," );
            Long[] lIds = new Long[sIds.length];
            for ( int i = 0; i < sIds.length; i++ ) {
                lIds[i] = Long.parseLong( sIds[i] );
            }

            this.demoEntityManager.removeByIds( lIds );

            this.setAjaxMessage( getText( "demoEntity.deleted", "no msg key found,delete successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.deleted", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }

        return this.SUCCESS;
    }

    public DemoEntityManager getDemoEntityManager() {
        return demoEntityManager;
    }

    public void setDemoEntityManager( DemoEntityManager demoEntityManager ) {
        this.demoEntityManager = demoEntityManager;
    }

    /*
     * public int getEditType() { return editType; } public void setEditType( int editType ) { this.editType = editType;
     * } public Long getId() { return id; } public void setId( Long id ) { this.id = id; } public String getIds() {
     * return ids; } public void setIds( String ids ) { this.ids = ids; } public DemoEntity getModel() { return model; }
     * public void setModel( DemoEntity demoEntity ) { this.model = demoEntity; }
     */

    private DemoEntityManager demoEntityManager;

    /*
     * private DemoEntity model = null; private Long id; private String ids; private int editType; protected static
     * final int EDITTYPE_EDIT = 0; protected static final int EDITTYPE_ADD = 1;
     */

}
